import axios from "axios";
const instance = axios.create({
	baseURL: "/api",
	timeout: 30000,
	headers: {
		"Content-Type": "application/json",
	},
});

instance.interceptors.request.use(config => {
	const token = "123";
	const headers = {};
	if (token) {
		headers.authorization = token;
	}
	return {
		...config,
		headers,
	};
});
instance.interceptors.response.use(
	response => {
		return response.data;
	},
	err => {
		switch (err.response.status) {
			case 401:
				alert("无权限");
				break;
			case 403:
				alert("权限过期");
				break;
			case 404:
				alert("接口不存在");
				break;
			default:
				alert("服务器错误");
				break;
		}
		return Promise.reject(err);
	}
);

export default instance;
